package com.origin.app.utils

import com.origin.app.MyApplication
import com.origin.netlibrary.NetUtils.getSuperNet
import com.origin.utils.gson.GsonUtils
import kotlinx.coroutines.CoroutineScope
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import retrofit2.Call
import java.lang.reflect.Type

object NetAppUtils {
    private var BASEURL = "http://132.232.253.210:25100"
    private var BASEURL_60126 = "http://132.232.253.210:60126"
    private var BASEURL_9528 = "http://132.232.253.210:9528"
     var staticFileHost = "http://132.232.253.210:60126"

    var loginUrl = "${BASEURL}/api/v1/user/login"
    var getTaskIdByPolygonUrl = "${BASEURL_60126}/api/v1/Task/GetTaskIdByPolygon"
    var getCompanyListUrl = "${BASEURL_60126}/api/v1/Company/getCompanyList"
    var getPointUrl = "${BASEURL_9528}/server/GetPoint"
    var getNotOnlineUrl = "${BASEURL_60126}/api/v1/Task/get_page_list"
    var getPageListUrl = "${BASEURL_60126}/api/v1/PersonInfo/Get_Page_List"
    var getLocationRecordsByTaskIdUrl = "${BASEURL_60126}/api/v1/Locationrecord/getLocationRecordsByTaskId"

    private fun headers(): Map<String, String> {

        var token =
            try {
                UserInfoUtils.getUserInfo().session_token
            }catch (e:Exception){
                ""
            }


        return mapOf(
            "Authorization" to token,
            "user-agent" to "Mozilla/5.0 (Linux; Android 12; GM1910 Build/SKQ1.211113.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/97.0.4692.98 Mobile Safari/537.36 uni-app Html5Plus/1.0 (Immersed/36.533333)",
            "package" to MyApplication.getInstance().packageName,
//            "channel" to AppChannelUtil.getAppChannel(),
            "Content-type" to "application/json",
//            "versionCode" to "${BuildConfig.VERSION_CODE}",
//            "oaid" to BaiduSemProxy.getOAID(),
        )
    }


    suspend fun <T> get(url: String, type: Type, viewModelScope: CoroutineScope): T {
        return privateGet(url).getSuperNet(viewModelScope, type)
    }


    suspend fun <T> postJson(url: String, bean: Any, type: Type, viewModelScope: CoroutineScope): T {

        return privatePostJson(url, bean).getSuperNet(viewModelScope, type)
    }

    private fun privateGet(url: String): Call<String> {
        return MyApplication.restService.get(url, headers = headers())

    }

    fun post(url: String, map: Map<String, String>): Call<String> {
        return MyApplication.restService.post(url, map, headers())
    }

    private fun privatePostJson(url: String, bean: Any): Call<String> {
        val json = GsonUtils.gson.toJson(bean)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json);
        return MyApplication.restService.postJson(url, requestBody, headers())
    }

    suspend   fun  <T> privatePostJsonStr(url: String, json: String,type: Type, viewModelScope: CoroutineScope): T {
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json);
        return MyApplication.restService.postJson(url, requestBody, headers()).getSuperNet(viewModelScope, type)
    }
}